package priv.bruce.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import priv.bruce.entry.MovieInfo;
import priv.bruce.service.MovieInfoService;

public class AddMovieServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private MovieInfoService movieInfoService = new MovieInfoService();

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json;charset=utf-8");
		String name = request.getParameter("movie_name");
		String director = request.getParameter("movie_director");
		String writer = request.getParameter("movie_writer");
		String actor = request.getParameter("movie_actor");
		String type = request.getParameter("movie_type");
		String region = request.getParameter("movie_region");
		String language = request.getParameter("movie_lang");
		String dateStr = request.getParameter("movie_date");
		String length = request.getParameter("movie_length");
		String alias = request.getParameter("movie_alias");
		String juqing = request.getParameter("movie_juqing");
		String cover = request.getParameter("movie_cover");
		int jishu = Integer.valueOf(request.getParameter("jishu"));
		try {
			List<MovieInfo> findByName = movieInfoService.findByName4admin(name);
			if (dateStr.equals("")) {
				response.getWriter().append(JSON.toJSONString(-2));
			}else {
				if (findByName.size() > 0) {
					if (findByName.get(0).getMovie_name().equals(name)) {
						response.getWriter().append(JSON.toJSONString(-1));
					}
				}else {
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
					Date date = sdf.parse(dateStr);
					int changeRows = movieInfoService.addMovie(name, director, writer, actor, type, region, language,
							date, length, alias, juqing, cover, jishu);
					System.out.println("影响行数："+changeRows);
					response.getWriter().append(JSON.toJSONString(changeRows));
				}
			}
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
}
